/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wzhq.commTencent;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import static com.wzhq.commTencent.PostTencent.GETRESULT;
import static com.wzhq.commTencent.PostTencent.URLREPORT;
import com.wzhq.pacsai.PacsAI;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONObject;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GG
 */
public class ReceiveReport implements Runnable {
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(ReceiveReport.class);
    String jobName;

    public ReceiveReport(String jobName) {
        this.jobName = jobName;
    }

    //从服务器取报告
    public String getReport(String Url, String interfaces) throws Exception {
        boolean isSuccess = false;
        String StudyInstanceUID = null;
        JSONObject postImages = new JSONObject();
        PostDcm report = new PostDcm();
        //向取条件：已PACS下载 且 未锁定 且 已上传图像 且 (未上传报告 或 报告内容为待处理 或 报告上传错误少于五次)
        List<Record> syncAIStatus = Db.find("select StudyInstanceUID from SyncAiStatus where SyncFlag=1 and UsingFlag=0 "
                + "and SyncAiFinshFlag=1 and (ReportFlag=0 or ReportResult=-1  or  ReportResult=0 or (ReportFlag=2 and ReportCounts<?)) "
                + "ORDER BY SyncTime ", PostTencent.MAXREOPER);
        if ((syncAIStatus == null) || (syncAIStatus.isEmpty())) {
            //无新图像
            return "无新上传图像要取报告！";
        } else {
            for (int j = 0; j < syncAIStatus.size(); j++) {
                StudyInstanceUID = syncAIStatus.get(j).getStr("StudyInstanceUID");
                //锁该条记录
                PostDcm.lockUsingFlag(StudyInstanceUID, 2);

                postImages.put("studyId", StudyInstanceUID);
                int i = 0;
                do {
                    i++;
                    isSuccess = report.Post2Tencent(postImages, Url, interfaces);
                } while ((!isSuccess) || i > PostTencent.MAXREOPER);
                //解锁该条记录
                PostDcm.lockUsingFlag(StudyInstanceUID, 0);
                System.out.println(postImages.toString());//输出JSON格式的postImages数据
            }
            return isSuccess ? "取到报告！" + StudyInstanceUID : "出现系统错误！" + StudyInstanceUID;
        }
    }

    @Override
    public void run() {
        String Message = "";
        System.out.println(jobName + " 启动：" + Calendar.getInstance().get(Calendar.SECOND));
        try {
            Message = this.getReport(PostTencent.URLREPORT, PostTencent.GETRESULT);
        } catch (Exception ex) {
            Message = "该进程出现错误！";
            LOG.error("从服务器取报告错误:");
            LOG.error(ex.toString());
            Logger.getLogger(ReceiveReport.class.getName()).log(Level.SEVERE, null, ex);
        }
        //Thread.sleep(2000);
        System.out.println(jobName + " 退出，结果：" + Message + Calendar.getInstance().get(Calendar.SECOND));
    }

}
